package com.yvtian.inv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yvtian.inv.pojo.*;
import com.yvtian.inv.dao.InvProductionAcceptanceMapper;
import com.yvtian.inv.service.InvOrderShopService;
import com.yvtian.inv.service.InvProductionAcceptanceItemService;
import com.yvtian.inv.service.InvProductionAcceptanceService;
import com.yvtian.inv.service.ShopService;
import com.yvtian.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 验收单(inv_production_acceptance)表服务实现类
 *
 * @author long
 * @since 2024-07-02 13:43:41
 */
@Service("invProductionAcceptanceService")
public class InvProductionAcceptanceServiceImpl extends ServiceImpl<InvProductionAcceptanceMapper, InvProductionAcceptanceEntity> implements InvProductionAcceptanceService {

    @Autowired
    private InvProductionAcceptanceItemService invProAccItemService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private InvOrderShopService invOrderShopService;

    @Override
    public IPage<InvProductionAcceptanceEntity> findByPage(Page<InvProductionAcceptanceEntity> page, InvProductionAcceptanceEntity invProductionAcceptanceEntity) {
        LambdaQueryWrapper<InvProductionAcceptanceEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InvProductionAcceptanceEntity::getIsDel, 0);
        getQueryWrapper(invProductionAcceptanceEntity, queryWrapper);
        return this.page(page, queryWrapper);
    }

    @Transactional
    @Override
    public boolean insertOrUpdateAll(InvProductionAcceptanceEntity invProductionAcceptanceEntity) {
        ArrayList<InvProductionAcceptanceItemEntity> invProAccItemList = invProductionAcceptanceEntity.getInvProAccItemList();
        if (invProAccItemList != null && !invProAccItemList.isEmpty()) {
            LambdaUpdateWrapper<InvProductionAcceptanceItemEntity> itemUpdateWrapper = new LambdaUpdateWrapper<>();
            itemUpdateWrapper.eq(InvProductionAcceptanceItemEntity::getZcode, invProductionAcceptanceEntity.getCode());
            invProAccItemService.remove(itemUpdateWrapper);
            invProAccItemService.saveBatch(invProAccItemList);
        }
        this.saveOrUpdate(invProductionAcceptanceEntity);
        return true;
    }

    @Transactional
    @Override
    public boolean handleStorage(InvProductionAcceptanceEntity invProEntity) {
        BigDecimal accQuantity = invProEntity.getAccQuantity();

        //生成入库单

        //更新商品库存
        LambdaUpdateWrapper<ShopEntity> shopEntityWrapper = new LambdaUpdateWrapper<>();
        shopEntityWrapper.setSql("quantity = quantity + " + accQuantity.doubleValue());
        shopEntityWrapper.eq(ShopEntity::getShopCode, invProEntity.getShopCode());
        shopService.update(shopEntityWrapper);

        //更新主表状态（审核）
        LambdaUpdateWrapper<InvProductionAcceptanceEntity> invProEntityUpdateWrapper = new LambdaUpdateWrapper<>();
        invProEntityUpdateWrapper.eq(InvProductionAcceptanceEntity::getCode, invProEntity.getCode());
        invProEntityUpdateWrapper.set(InvProductionAcceptanceEntity::getStatus, 1);
        invProEntityUpdateWrapper.set(InvProductionAcceptanceEntity::getCheckName, invProEntity.getCheckName());
        invProEntityUpdateWrapper.set(InvProductionAcceptanceEntity::getCheckUser, invProEntity.getCheckUser());
        invProEntityUpdateWrapper.set(InvProductionAcceptanceEntity::getCheckTime, new Date());
        this.update(invProEntityUpdateWrapper);
        return true;
    }

    @Override
    public ArrayList<InvProductionAcceptanceItemEntity> computeMaterial(InvProductionAcceptanceEntity invProEntity) {
        ArrayList<InvProductionAcceptanceItemEntity> proItemList = new ArrayList<>();
        //查询生产订单的用料明细
        List<InvOrderShopEntity> invOrderItemList = invOrderShopService.list(Wrappers.<InvOrderShopEntity>lambdaQuery()
                .eq(InvOrderShopEntity::getZcode, invProEntity.getInvOrderCode()));

        //设置用料明细
        for (InvOrderShopEntity invOrderShop : invOrderItemList) {
            InvProductionAcceptanceItemEntity invProItem = new InvProductionAcceptanceItemEntity();
            invProItem.setZcode(invProEntity.getCode());
            invProItem.setRequiredQuantity(invOrderShop.getRequiredQuantity());
            invProItem.setReceiveCount(invOrderShop.getReceiveCount());
            invProItem.setUsedCount(invOrderShop.getUsedCount());
            invProItem.setReturnCount(invOrderShop.getReturnCount());
            invProItem.setShopName(invOrderShop.getShopName());
            invProItem.setShopCode(invOrderShop.getShopCode());
            invProItem.setAttribute(invOrderShop.getAttribute());
            invProItem.setUnit(invOrderShop.getUnit());
        }

        return proItemList;
    }

    private void getQueryWrapper(InvProductionAcceptanceEntity invProductionAcceptanceEntity, LambdaQueryWrapper<InvProductionAcceptanceEntity> queryWrapper) {
        queryWrapper
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getCode()), InvProductionAcceptanceEntity::getCode, invProductionAcceptanceEntity.getCode())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getInvOrderCode()), InvProductionAcceptanceEntity::getInvOrderCode, invProductionAcceptanceEntity.getInvOrderCode())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getStockCode()), InvProductionAcceptanceEntity::getStockCode, invProductionAcceptanceEntity.getStockCode())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getStockName()), InvProductionAcceptanceEntity::getStockName, invProductionAcceptanceEntity.getStockName())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getProjectCode()), InvProductionAcceptanceEntity::getProjectCode, invProductionAcceptanceEntity.getProjectCode())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getProjectName()), InvProductionAcceptanceEntity::getProjectName, invProductionAcceptanceEntity.getProjectName())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getShopCode()), InvProductionAcceptanceEntity::getShopCode, invProductionAcceptanceEntity.getShopCode())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getShopName()), InvProductionAcceptanceEntity::getShopName, invProductionAcceptanceEntity.getShopName())
                .eq(invProductionAcceptanceEntity.getAccQuantity() != null, InvProductionAcceptanceEntity::getAccQuantity, invProductionAcceptanceEntity.getAccQuantity())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getAttribute()), InvProductionAcceptanceEntity::getAttribute, invProductionAcceptanceEntity.getAttribute())
                .eq(invProductionAcceptanceEntity.getUnit() != null, InvProductionAcceptanceEntity::getUnit, invProductionAcceptanceEntity.getUnit())
                .eq(invProductionAcceptanceEntity.getPrice() != null, InvProductionAcceptanceEntity::getPrice, invProductionAcceptanceEntity.getPrice())
                .eq(invProductionAcceptanceEntity.getAmount() != null, InvProductionAcceptanceEntity::getAmount, invProductionAcceptanceEntity.getAmount())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getAccUsername()), InvProductionAcceptanceEntity::getAccUsername, invProductionAcceptanceEntity.getAccUsername())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getAccAccount()), InvProductionAcceptanceEntity::getAccAccount, invProductionAcceptanceEntity.getAccAccount())
                .eq(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getIsInbound()), InvProductionAcceptanceEntity::getIsInbound, invProductionAcceptanceEntity.getIsInbound())
                .eq(invProductionAcceptanceEntity.getStatus() != null, InvProductionAcceptanceEntity::getStatus, invProductionAcceptanceEntity.getStatus())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getDelName()), InvProductionAcceptanceEntity::getDelName, invProductionAcceptanceEntity.getDelName())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getCreateName()), InvProductionAcceptanceEntity::getCreateName, invProductionAcceptanceEntity.getCreateName())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getCreateUser()), InvProductionAcceptanceEntity::getCreateUser, invProductionAcceptanceEntity.getCreateUser())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getUpdateName()), InvProductionAcceptanceEntity::getUpdateName, invProductionAcceptanceEntity.getUpdateName())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getUpdateUser()), InvProductionAcceptanceEntity::getUpdateUser, invProductionAcceptanceEntity.getUpdateUser())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getRemarks()), InvProductionAcceptanceEntity::getRemarks, invProductionAcceptanceEntity.getRemarks())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getCheckName()), InvProductionAcceptanceEntity::getCheckName, invProductionAcceptanceEntity.getCheckName())
                .like(StringHelper.isNotEmpty(invProductionAcceptanceEntity.getCheckUser()), InvProductionAcceptanceEntity::getCheckUser, invProductionAcceptanceEntity.getCheckUser());
    }
}

